package io.gitee.crud.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult<T> {

    private List<T> list;      // 当前页数据
    private long total;        // 总记录数
    private int pageNum;       // 当前页
    private int pageSize;      // 每页大小
    private int pages;         // 总页数
    private boolean hasNext;    // 是否还有下一页
    private boolean hasPrev;    // 是否还有上一页

    public static <T> PageResult<T> of(IPage<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setList(page.getRecords());
        result.setTotal(page.getTotal());
        result.setPageNum((int) page.getCurrent());
        result.setPageSize((int) page.getSize());
        result.setPages((int) page.getPages());
        result.setHasNext(page.getCurrent() < page.getPages());
        result.setHasPrev(page.getCurrent() > 1);
        return result;
    }
}
